import java.util.Stack;

public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        int index =0;
        Stack<Integer> stack = new Stack<>();
        for(int i =0;i<pushA.length;++i){
            stack.push(pushA[i]);
            while(!stack.empty()&&index<popA.length&&stack.peek()==popA[index]){
                stack.pop();
                index++;
            }
            
        }
        return stack.empty();
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        //[1,2,3,4,5],[4,5,3,2,1]
        int[] pushA = {1,2,3,4,5};
        int[] popA = {4,5,3,2,1};
        System.out.println(solution.IsPopOrder(pushA, popA));
    }

}